var util = require('../../utils/util.js');
function initSubMenuDisplay() {
  return ['hidden', 'hidden'];
}
var app = getApp();
let goodsList = [];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    subMenuDisplay:initSubMenuDisplay(),
    num:1,
  },
  pageIndex: 1, 
  pageSize:100,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var hrefNumber = options.hrefNum;
    const userid = app.globalData.userid;
    var that = this;
    if(hrefNumber == 1){
      wx.request({
        url: app.globalData.url + 'getOrderSelective',
        data: {
          userId: userid,
          pageSize: that.pageSize,
          pageIndex: that.pageIndex,
          payStatus: 0
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        method: 'GET',
        success: function (res) {
          if (res.data.resultMsg == "没有查到订单数据信息") {
            that.setData({
              order: '',
              num: hrefNumber
            })
          } else {
            that.setData({
              order: res.data.object.orderList,
              num: hrefNumber
            })
          }
          goodsList = res.data.object.orderList.filter(goods => {
            return goods.orderType == 2
          })
        },
        fail: function (res) { },
        complete: function (res) { },
      })
    }else if(hrefNumber == 2){
      wx.request({
        url: app.globalData.url + 'getOrderSelective',
        data: {
          userId: userid,
          pageSize: that.pageSize,
          pageIndex: that.pageIndex,
          shippingStatus: 0
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        method: 'GET',
        success: function (res) {
          if (res.data.resultMsg == "没有查到订单数据信息") {
            that.setData({
              order: '',
              num: hrefNumber
            })
          } else {
            that.setData({
              order: res.data.object.orderList,
              num:hrefNumber
            })
          }
        },
        fail: function (res) { },
        complete: function (res) { },
      })
    }else if(hrefNumber == 3){
      wx.request({
        url: app.globalData.url + 'getOrderSelective',
        data: {
          userId: userid,
          pageSize: that.pageSize,
          pageIndex: that.pageIndex,
          shippingStatus: 1
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        method: 'GET',
        success: function (res) {
          if (res.data.resultMsg == "没有查到订单数据信息") {
            that.setData({
              order: '',
              num: hrefNumber
            })
          } else {
            that.setData({
              order: res.data.object.orderList,
              num: hrefNumber
            })
          }
        },
        fail: function (res) { },
        complete: function (res) { },
      })
    }else if(hrefNumber == 4){
      wx.request({
        url: app.globalData.url + 'getOrderSelective',
        data: {
          userId: userid,
          pageSize: that.pageSize,
          pageIndex: that.pageIndex,
          shippingStatus: 2
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        method: 'GET',
        success: function (res) {
          if (res.data.resultMsg == "没有查到订单数据信息") {
            that.setData({
              order: '',
              num: hrefNumber
            })
          } else {
            that.setData({
              order: res.data.object.orderList,
              num: hrefNumber
            })
          }
        },
        fail: function (res) { },
        complete: function (res) { },
      })
    }else if(hrefNumber == 5){
      wx.request({
        url: app.globalData.url + 'getOrderSelective',
        data: {
          userId: userid,
          pageSize: that.pageSize,
          pageIndex: that.pageIndex,
          shippingStatus: 5
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        method: 'GET',
        success: function (res) {
          if (res.data.resultMsg == "没有查到订单数据信息") {
            that.setData({
              order: '',
              num: hrefNumber
            })
          } else {
            that.setData({
              order: res.data.object.orderList,
              num: hrefNumber
            })
          }
        },
        fail: function (res) { },
        complete: function (res) { },
      })
    }else if(hrefNumber == 0){
      wx.request({
      url: app.globalData.url + 'getOrderAll',
      data: {
        userId: userid,
        pageSize: that.pageSize,
        pageIndex: that.pageIndex,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'GET',
      success: function(res) {
        that.setData({
          order: res.data.object.orderList,
          num: hrefNumber
        })
        goodsList = res.data.object.orderList.filter(goods => {
          return goods.orderType == 2  
        })
      },
      fail: function(res) {},
      complete: function(res) {},
    })
    }
  },
  mineOrder:function(e){
    var index = parseInt(e.currentTarget.dataset.index);
    var newSubMenuDisplay = initSubMenuDisplay();
    if (this.data.subMenuDisplay[index] == 'hidden') {
      newSubMenuDisplay[index] = 'show';
    } else {
      newSubMenuDisplay[index] = 'hidden';
    }
    this.setData({
      subMenuDisplay: newSubMenuDisplay,
    });
  },
  /**
   * 商城----状态按钮
   */
  orderButton:function(e){
    if (!this.data.buttonClicked) {
      const shippingStatus = e.currentTarget.dataset.shippingstatus
      const payStatus = e.currentTarget.dataset.paystatus
      util.buttonClicked(this);
      if (payStatus == 2 && shippingStatus == 0 || shippingStatus == 3) {
        wx.showToast({
          title: '已提醒卖家发货',
          icon: 'success',
          duration: 1000,
          mask: true,
          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { },
        })
      } else if (shippingStatus == 1) {
        //物流
      } else if (shippingStatus == 2) {
        wx.navigateTo({
          url: '../orderConfirm/orderConfirm?shippingStatus=' + 2,
          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { },
        })
      } else if (payStatus == 0) {
        const goods = [e.currentTarget.dataset.goods]
        const list = JSON.stringify(goods)
        wx.navigateTo({
          url: '../orderPay/orderPay?goodsList=' + list,
          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { },
        })
      }
    }
  },
  /**
   * 筛选订单
   */
  cantenOrder:function(e){
    var that = this;
    const cantenOrder = e.currentTarget.dataset.index;
    const userid = app.globalData.userid;
    wx.request({
      url: app.globalData.url + 'getOrderSelective',
      data: {
        userId: userid,
        pageSize: that.pageSize,
        pageIndex: that.pageIndex,
        orderType: cantenOrder
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'GET',
      success: function(res) {
        that.setData({
          order: res.data.object.orderList,
          num:1
        })
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  //商城
  shoppingOrder: function (e) {
    var that = this;
    const shoppingOrder = e.currentTarget.dataset.index;
    const userid = app.globalData.userid;
    wx.request({
      url: app.globalData.url + 'getOrderSelective',
      data: {
        userId: userid,
        pageSize: that.pageSize,
        pageIndex: that.pageIndex,
        orderType: shoppingOrder
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'GET',
      success: function (res) {
        that.setData({
          order: res.data.object.orderList,
          num:1
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  /**
   * 点击切换，筛选项
   */
  allScreen:function(e){
    var that = this;
    const userid = app.globalData.userid;
    wx.request({
      url: app.globalData.url + 'getOrderAll',
      data: {
        userId: userid,
        pageSize: that.pageSize,
        pageIndex: that.pageIndex,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'GET',
      success: function (res) {
        that.setData({
          order: res.data.object.orderList,
          num: e.currentTarget.dataset.num
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  }, 
  DScreen: function (e) {
    var that = this;
    const paystatus = e.currentTarget.dataset.paystatus;
    const userid = app.globalData.userid;
    wx.request({
      url: app.globalData.url + 'getOrderSelective',
      data: {
        userId: userid,
        pageSize: that.pageSize,
        pageIndex: that.pageIndex,
        payStatus: paystatus
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'GET',
      success: function (res) {
        if (res.data.resultMsg == "没有查到订单数据信息"){
          that.setData({
            order:'',
            num: e.currentTarget.dataset.num 
          })
        }else{
          that.setData({
              order: res.data.object.orderList,
              num: e.currentTarget.dataset.num       
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  }, 
  FScreen: function (e) {
    var that = this;
    const shippingStatus = e.currentTarget.dataset.shippingstatus
    const paystatus = e.currentTarget.dataset.paystatus;
    const userid = app.globalData.userid;
    wx.request({
      url: app.globalData.url + 'getOrderSelective',
      data: {
        userId: userid,
        pageSize: that.pageSize,
        pageIndex: that.pageIndex,
        shippingStatus: shippingStatus,
        payStatus: paystatus
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'GET',
      success: function (res) {
        console.log(that.order)
        if (res.data.resultMsg == "没有查到订单数据信息") {
          that.setData({
            order: '',
            num: e.currentTarget.dataset.num
          })
        } else {
          that.setData({
            order: res.data.object.orderList,
            num: e.currentTarget.dataset.num
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  DsScreen: function (e) {
    var that = this;
    const shippingStatus = e.currentTarget.dataset.shippingstatus
    const userid = app.globalData.userid;
    wx.request({
      url: app.globalData.url + 'getOrderSelective',
      data: {
        userId: userid,
        pageSize: that.pageSize,
        pageIndex: that.pageIndex,
        shippingStatus: shippingStatus
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'GET',
      success: function (res) {
        if (res.data.resultMsg == "没有查到订单数据信息") {
          that.setData({
            order: '',
            num: e.currentTarget.dataset.num
          })
        } else {
          that.setData({
            order: res.data.object.orderList,
            num: e.currentTarget.dataset.num
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  appraiseScreen: function (e) {
    var that = this;
    const shippingStatus = e.currentTarget.dataset.shippingstatus
    const userid = app.globalData.userid;
    wx.request({
      url: app.globalData.url + 'getOrderSelective',
      data: {
        userId: userid,
        pageSize: that.pageSize,
        pageIndex: that.pageIndex,
        shippingStatus: shippingStatus
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'GET',
      success: function (res) {
        if (res.data.resultMsg == "没有查到订单数据信息") {
          that.setData({
            order: '',
            num: e.currentTarget.dataset.num
          })
        } else {
          that.setData({
            order: res.data.object.orderList,
            num: e.currentTarget.dataset.num
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  TScreen: function (e) {
    var that = this;
    const shippingStatus = e.currentTarget.dataset.shippingstatus
    const userid = app.globalData.userid;
    wx.request({
      url: app.globalData.url + 'getOrderSelective',
      data: {
        userId: userid,
        pageSize: that.pageSize,
        pageIndex: that.pageIndex,
        shippingStatus: shippingStatus
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'GET',
      success: function (res) {
        if (res.data.resultMsg == "没有查到订单数据信息") {
          that.setData({
            order: '',
            num: e.currentTarget.dataset.num
          })
        } else {
          that.setData({
            order: res.data.object.orderList,
            num: e.currentTarget.dataset.num
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  /**
   * 食堂按钮
   */
  againOrder: function (e) {
    const payStatus = e.currentTarget.dataset.paystatus
    const shippingStatus = e.currentTarget.dataset.shippingstatus
    if (payStatus == 0 || shippingStatus == 0) {
      //支付
      const goods = [e.currentTarget.dataset.goods]
      const list = JSON.stringify(goods)
      wx.navigateTo({
        url: '../shouye/makeorder/makeorder?list=' + list,
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    } else {
      //再来一单
      wx.switchTab({
        url: '../shouye/index/index',
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
  },
  /**
   * 食堂确认支付
   */
  confirmPay:function(){
    wx.navigateTo({
      url: '../shouye/makeorder/makeorder',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  /**
   * 订单详情--食堂
   */
  stDetail:function(){
    wx.navigateTo({
      url: '../mine/order/detail/detail',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  /**
   * 商品详情
   */
  goodsDetail:function(e){
    const payStatus = e.currentTarget.dataset.paystatus
    const goodsId = e.currentTarget.dataset.goodsid
    const goods = [e.currentTarget.dataset.goods]
    const goodsList = JSON.stringify(goods)
    if (payStatus == 0){
       wx.navigateTo({
         url: '../commodityDetail/commodityDetail?goodsId='+goodsId,
         success: function(res) {},
         fail: function(res) {},
         complete: function(res) {},
       })
    } else if (payStatus == 2){
        wx.navigateTo({
        url: '../orderDetail/orderDetail?goodsList=' + goodsList,
        success: function(res) {},
        fail: function(res) {},
        complete: function(res) {},
      })
    }
  }
})